package com.dreamstations.changshads.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.entity.BaseBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.L;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/*
使用 webview 来显示文章内容
 */
public class ArticleDetail_WebActivity extends AppCompatActivity {
    private final static boolean ISDEV = false;
    private final static String TAG = ArticleDetail_WebActivity.class.getName();

    private WebView mWebView;
    private ImageView titlePic;
    private TextView titleTextView;
    private String title;
    private String content;
    private String cover;

    private Picasso picasso;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acticle_detail_webview);
        picasso = Picasso.with(getApplicationContext());

        Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //初始化WebView
        mWebView = (WebView) findViewById(R.id.article_webview);
        mWebView.setInitialScale(300);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDisplayZoomControls(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");

        titlePic = (ImageView) findViewById(R.id.detail_tool_img);

        if (ISDEV) {
            getDatasFromFile();
        } else {
            loadArticle();
        }
//        cover=null;
        if (cover != null && !cover.equals("")) {
            L.i(TAG, "=>loadPic:" + DSRequest.SERVER_BASE1 + cover);
            picasso.load(DSRequest.SERVER_BASE1 + cover)
                    .placeholder(R.drawable.img_default)
                    .error(R.drawable.img_default)
                    .tag(getApplication())
                    .into(titlePic);
        }
        titleTextView = (TextView) findViewById(R.id.detail_title);
        titleTextView.setText(title);
        setTitle(title);
        L.i(TAG, "=>loadWebView:" + content);
        mWebView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
    }

    private void loadArticle() {
        BaseBean o = null;
        try {
            o = (BaseBean) getIntent().getSerializableExtra("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (o == null) {
            Toast.makeText(getApplicationContext(), "加载文章失败，请刷新重试", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        title = o.getTitle();
        cover = o.getCover();
        content = o.getContent();
    }

    private void getDatasFromFile() {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            String line;
            InputStream inputStream = getAssets().open("temp.txt");
            BufferedReader reader
                    = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        L.d(stringBuilder.toString());
        title = "毛泽东的抗日国际统一战线思想及其历史贡献";
        content = stringBuilder.toString();
    }

    @Override
    protected void onPause() {
        picasso.cancelTag(getApplication());
        super.onPause();
    }
}
